package com.ruoyi.web.controller.system.api.cc;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.interceptor.Authentication;
import com.ruoyi.system.api.ResponseWithData;
import com.ruoyi.system.api.domain.StServiceReport;
import com.ruoyi.system.api.service.CcService;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.service.ISysDictDataService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "CC字典")
@Controller
@RequestMapping("/api/cc/dict/")
public class CCDictController {

    @Autowired
    private CcService ccService;

    @Autowired
    private ISysDictDataService sysDictDataService;

    private static final Logger log = LoggerFactory.getLogger(CCDictController.class);

    @GetMapping("selectCCDict")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "CC根据TableName查询数据", httpMethod = "GET", notes = "CC根据TableName查询数据")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "tableName", name = "tableName", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "engineerId", name = "engineerId", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData selectCCDict(@RequestParam(value = "tableName") String tableName, @RequestParam(value = "engineerId") Long engineerId, @RequestParam(value = "parentValue", required = false) String parentValue) {
        try {
            List paramTables = null;
            if (StringUtils.isEmpty(parentValue)) {
                paramTables = ccService.getParamTables(tableName, engineerId);
            } else {
                paramTables = ccService.selectCCDictWidthParent(tableName, engineerId, parentValue);
            }
            return new ResponseWithData().success(paramTables).code(200);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常！").code(500);
        }
    }

    /**
     * 查询联动cc字典，parentValue：父类型key
     *
     * @param
     * @return com.ruoyi.system.api.ResponseWithData
     * @author wlf
     * @date 2020/5/13 9:8
     */
    @GetMapping("selectCCDictWidthParent")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "CC根据TableName查询数据", httpMethod = "GET", notes = "CC根据TableName查询数据")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "tableName", name = "tableName", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "engineerId", name = "engineerId", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "parentValue", name = "parentValue", required = false, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData selectCCDictWidthParent(@RequestParam(value = "tableName") String tableName, @RequestParam(value = "engineerId") Long engineerId, @RequestParam(value = "parentValue") String parentValue) {
        try {
            List paramTables = ccService.selectCCDictWidthParent(tableName, engineerId, parentValue);
            return new ResponseWithData().success(paramTables).code(200);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常！").code(500);
        }
    }

    @GetMapping("selectDict")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "查询系统字典数据", httpMethod = "GET", notes = "查询系统字典数据")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "dictType", name = "dictType", required = true, dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData selectDict(@RequestParam(value = "dictType") String dictType) {
        try {
            SysDictData sysDictData = new SysDictData();
            sysDictData.setDictType(dictType);

            List<SysDictData> list = sysDictDataService.selectDictDataList(sysDictData);

            List<Map> listMap  = new ArrayList();
            if(StringUtils.equals("parttype_report",dictType)||StringUtils.equals("parttype_baogao",dictType)){
                for(SysDictData sysDictData1:list){
                    Map<String,String> map = new HashMap();
                    map.put("text",sysDictData1.getDictLabel());
                    map.put("value",sysDictData1.getDictValue());
                    listMap.add(map);
                }
                return new ResponseWithData().success(listMap).msg("查询成功").code(200);
            }
            return new ResponseWithData().success(list).msg("查询成功").code(200);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常！").code(500);
        }
    }

    /**
     * 查询系统字典数据hot_account_typet
     *
     * @param
     * @return com.ruoyi.system.api.ResponseWithData
     * @author yzg
     * @date 2020/4/17 2:46 下午
     */
    @GetMapping("selectHotAccountType")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "查询系统字典数据hot_account_typet", httpMethod = "GET", notes = "查询系统字典数据hot_account_type")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "engineerId", name = "engineerId", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData selectHotAccountType(@RequestParam(value = "engineerId") Long engineerId) {
        try {
            List list = sysDictDataService.selectHotAccountType(engineerId);
            return new ResponseWithData().success(list).msg("查询成功").code(200);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常！").code(500);
        }
    }

    /**
     * 查询系统字典数据fail_type
     *
     * @param
     * @return com.ruoyi.system.api.ResponseWithData
     * @author yzg
     * @date 2020/4/17 2:46 下午
     */
    @GetMapping("selectCCFailtype")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "查询系统字典数据fail_type", httpMethod = "GET", notes = "查询系统字典数据fail_type")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "engineerId", name = "engineerId", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData selectCCFailtype(@RequestParam(value = "engineerId") Long engineerId) {
        try {
            List list = sysDictDataService.selectCCFailtype(engineerId);
            return new ResponseWithData().success(list).msg("查询成功").code(200);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常！").code(500);
        }
    }

    /**
     * 查询系统字典数据equipment_model
     *
     * @param
     * @return com.ruoyi.system.api.ResponseWithData
     * @author yzg
     * @date 2020/4/17 2:46 下午
     */
    @GetMapping("selectCCEquipmentModel")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "查询系统字典数据equipment_model", httpMethod = "GET", notes = "查询系统字典数据equipment_model")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "engineerId", name = "engineerId", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData selectCCEquipmentModel(@RequestParam(value = "engineerId") Long engineerId) {
        try {
            List list = sysDictDataService.selectCCEquipmentModel(engineerId);
            return new ResponseWithData().success(list).msg("查询成功").code(200);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常！").code(500);
        }
    }

    /**
     * 查询系统字典数据instrument_status
     *
     * @param
     * @return com.ruoyi.system.api.ResponseWithData
     * @author yzg
     * @date 2020/4/17 2:46 下午
     */
    @GetMapping("selectCCInstrumentStatus")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "查询系统字典数据instrument_status", httpMethod = "GET", notes = "查询系统字典数据instrument_status")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "engineerId", name = "engineerId", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData selectCCInstrumentStatus(@RequestParam(value = "engineerId") Long engineerId) {
        try {
            List list = sysDictDataService.selectCCInstrumentStatus(engineerId);
            return new ResponseWithData().success(list).msg("查询成功").code(200);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常！").code(500);
        }
    }

    /**
     * 查询系统字典数据HTSeverity severityLevel
     *
     * @param
     * @return com.ruoyi.system.api.ResponseWithData
     * @author yzg
     * @date 2020/4/17 2:46 下午
     */
    @GetMapping("selectCCHTSeverity")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "查询系统字典数据HTSeverity severityLevel", httpMethod = "GET", notes = "查询系统字典数据HTSeverity severityLevel")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "engineerId", name = "engineerId", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData selectCCHTSeverity(@RequestParam(value = "engineerId") Long engineerId) {
        try {
            List list = sysDictDataService.selectCCHTSeverity(engineerId);
            return new ResponseWithData().success(list).msg("查询成功").code(200);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常！").code(500);
        }
    }

    /**
     * 获取ErrorCode三级下拉
     *
     * @param parentValue
     * @param errCode
     * @methodName: getParamTablesErr
     * @description: TODO
     * @return: com.ruoyi.system.api.ResponseWithData<?>
     * @author: shihx
     * @date: 2020-04-10 10:26
     **/
    @GetMapping("/getParamTablesErr")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "获取ErrorCode三级下拉", httpMethod = "GET", notes = "获取ErrorCode三级下拉")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "Err代码类别", name = "parentValue", required = true, defaultValue = "001", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "Err代码", name = "errCode", required = false, defaultValue = "001", dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功", response = StServiceReport.class)
    })
    public ResponseWithData<?> getParamTablesErr(@RequestParam(value = "parentValue", required = true) String parentValue,
                                                 @RequestParam(value = "errCode", required = false) String errCode
    ) {
        try {
            return ccService.getParamTablesErr(parentValue, errCode);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }


}
